package cn.com.nxt.txb.adapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.telecom.Call;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import cn.com.nxt.txb.R;
import cn.com.nxt.txb.model.CallRecord;
import cn.com.nxt.txb.util.OnRecyclerItemClickListener;

public class CallRecordAdapter extends RecyclerView.Adapter<CallRecordAdapter.MyViewHolder> {

	public CallRecordAdapter(Context mContext, List<CallRecord> mDatas) {
		super();
		this.mContext = mContext;
		this.mDatas = mDatas;
	}

	private Context mContext;
	protected List<CallRecord> mDatas;

	private OnRecyclerItemClickListener mOnItemClickListener;

	public void setOnItemClickListener(OnRecyclerItemClickListener listener) {
		this.mOnItemClickListener = listener;
	}

	@Override
	public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
		MyViewHolder holder = new MyViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_call_record, parent, false));
		return holder;
	}

	@Override
	public void onBindViewHolder(final MyViewHolder holder, final int position) {
		CallRecord record=mDatas.get(position);
		if(record.getDirect()==2){
			holder.ivType.setImageResource(R.drawable.call_out);
		}else if(record.getDirect()==1&&record.getStatus()==0){
			holder.ivType.setImageResource(R.drawable.incoming_call);
		}else if(record.getDirect()==1&&record.getStatus()==1){
			holder.ivType.setImageResource(R.drawable.missed_calls);
		}
		holder.tvName.setText(record.getName());
		holder.tvphone.setText(record.getPhone());
		holder.tvTime.setText(stampToDate(record.getCreate_at()));
		// 绑定每一个itemView的事件  抽取方法 供子类复用
		setItemViewEvent(holder);
	}

	protected void setItemViewEvent(final MyViewHolder holder) {
		if (mOnItemClickListener != null) {
			holder.itemView.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					int realPos = holder.getLayoutPosition();
					mOnItemClickListener.OnItemClick(holder.itemView, realPos);
				}
			});
		}
	}

	@Override
	public int getItemCount() {
		return mDatas.size();
	}

//	void louisAdd(int pos) {
//		mDatas.add(pos, "插入1条数据1");
//		this.notifyItemInserted(pos);
//	}

	void louisDelete(int pos) {
		mDatas.remove(pos);
		this.notifyItemRemoved(pos);
	}

	class MyViewHolder extends ViewHolder {
		ImageView ivType;
		TextView tvName;
		TextView tvphone;
		TextView tvTime;

		public MyViewHolder(View view) {
			super(view);
			ivType=(ImageView) view.findViewById(R.id.iv_type);
			tvName = (TextView) view.findViewById(R.id.tv_name);
			tvphone = (TextView) view.findViewById(R.id.tv_phone);
			tvTime = (TextView) view.findViewById(R.id.tv_time);
		}
	}
	/*
     * 将时间戳转换为时间
     */
	public static String stampToDate(String s){
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm");// HH:mm:ss
		long lt = new Long(s+"000");
		Date date = new Date(lt);
		res = simpleDateFormat.format(date);
		return res;
	}

}
